<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initAutoLoad() {
        $autoLoader = Zend_Loader_Autoloader::getInstance();
        if (@class_exists('Vlib_Module_Loader')) {
            $modules = Vlib_Module_Loader::getIntance()->getModules();
            if ($modules) {
                foreach ($modules as $module) {
                    new Vlib_Loader($config = array(
                                'basePath' => APPLICATION_PATH . DS . 'modules' . DS . $module,
                                'namespace' => ucfirst($module) . '_'
                            ));
                }
            }
        }
        return $autoLoader;
    }

    public function _initView() {
        // get application config
        $config = Vlib_Config::getConfig();

        // init view
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        if (null == $viewRenderer->view)
            $viewRenderer->initView();
        $view = $viewRenderer->view;

        if (is_object($view)) {
            $view->doctype('XHTML1_STRICT');

            // set application charset
            $charet = $config['web']['charset'] ? $config['web']['charset'] : 'UTF-8';
            $view->charset = $charet;

            // build the root url
            $request = new Zend_Controller_Request_Http();
            $siteUrl = $request->getScheme() . '://' . $request->getHttpHost();
            $view->host = $siteUrl;
            $basePath = $request->getBasePath();
            $siteUrl = ($basePath == '') ? $siteUrl : $siteUrl . '/' . ltrim($basePath, '/');
            $view->baseUrl = $siteUrl;
            $siteUrl = $siteUrl . '/index.php';
            $view->rootUrl = $siteUrl;
        }
    }

    protected function _initInstallChecker() {
        $config = Vlib_Config::getConfig();
        // check if application is not installed, redirect to install page
        if (!$config || !@array_key_exists('install', $config)
                || null == $config['install']
                || null == $config['install']['date']) {
            // build the root url
            $request = new Zend_Controller_Request_Http();
            $siteUrl = $request->getScheme() . '://' . $request->getHttpHost();
            $basePath = $request->getBasePath();
            $siteUrl = ($basePath == '') ? $siteUrl : $siteUrl . '/' . ltrim($basePath, '/');

            header('Location: ' . $siteUrl . '/install.php');
            exit;
        }
    }

    public function _initPlugin() {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Vlib_Plugin_Layout())
                ->registerPlugin(new Vlib_Plugin_Language());
    }

}